package com.schoolgard.action.remoteOperator;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.schoolegard.services.DeviceService;
import com.schoolegard.services.RemoteOperatorService;
import com.schoolegard.services.UpgradeToDeviceService;
import com.schoolgard.base.BaseAction;
import com.schoolgard.entitybean.CardLogToDevice;
import com.schoolgard.entitybean.Device;
import com.schoolgard.entitybean.GridDataModel;
import com.schoolgard.entitybean.PictureLogToDevice;
import com.schoolgard.entitybean.TimingToDevice;
import com.schoolgard.entitybean.Users;
import com.supersystem.utils.DateJsonValueProcessor;

public class RemoteOperatorAction extends BaseAction{
    
	private static Logger logger = Logger.getLogger(RemoteOperatorAction.class);
	private static final long serialVersionUID = 201112052117L;
	private RemoteOperatorService remoteOperatorService;
	private UpgradeToDeviceService upgradeToDeviceService;
	private DeviceService deviceService;
	private CardLogToDevice catd;
	private PictureLogToDevice pitd;
	private TimingToDevice titd;
	public void setRemoteOperatorService(RemoteOperatorService remoteOperatorService) {
		this.remoteOperatorService = remoteOperatorService;
	}
	public void setUpgradeToDeviceService(
			UpgradeToDeviceService upgradeToDeviceService) {
		this.upgradeToDeviceService = upgradeToDeviceService;
	}

	public void setDeviceService(DeviceService deviceService) {
		this.deviceService = deviceService;
	}
	public void findCardToDevice()
	{
		String startStr = request.getParameter("start"); // grid传过来的当前数据条数
		String limitStr = request.getParameter("limit"); // 每页显示的数据数

		int start = Integer
				.parseInt(StringUtils.isNotBlank(startStr) ? startStr : "0");
		int limit = Integer
				.parseInt(StringUtils.isNotBlank(limitStr) ? limitStr : "30");
		int end = start + limit; // 当前页需要显示的数据 start--> end
		List<CardLogToDevice> cardLs = remoteOperatorService.findCardToDevice();
		int total = cardLs.size();
		if (end > total)
			end = total;

		JsonValueProcessor jsonProcessor = new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss");
		JsonConfig config = new JsonConfig(); 
		config.registerJsonValueProcessor(Date.class, jsonProcessor);

		GridDataModel<CardLogToDevice> page = new GridDataModel<CardLogToDevice>();
		page.setPage(start / limit + 1);
		page.setRows(cardLs);
		page.setTotal(total);
		String jsonStr = JSONObject.fromObject(page, config).toString();
		writeToResponse(jsonStr);
	}
	public void findImgToDevice()
	{
		String startStr = request.getParameter("start"); // grid传过来的当前数据条数
		String limitStr = request.getParameter("limit"); // 每页显示的数据数
		
		int start = Integer
		.parseInt(StringUtils.isNotBlank(startStr) ? startStr : "0");
		int limit = Integer
		.parseInt(StringUtils.isNotBlank(limitStr) ? limitStr : "30");
		int end = start + limit; // 当前页需要显示的数据 start--> end
		List<PictureLogToDevice> cardLs = remoteOperatorService.findPictureLogToDevice();
		int total = cardLs.size();
		if (end > total)
			end = total;

		JsonValueProcessor jsonProcessor = new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss");
		JsonConfig config = new JsonConfig(); 
		config.registerJsonValueProcessor(Date.class, jsonProcessor);
		
		GridDataModel<PictureLogToDevice> page = new GridDataModel<PictureLogToDevice>();
		page.setPage(start / limit + 1);
		page.setRows(cardLs);
		page.setTotal(total);
		String jsonStr = JSONObject.fromObject(page, config).toString();
		writeToResponse(jsonStr);
	}
	public void findTimingToDevice()
	{
		String startStr = request.getParameter("start"); // grid传过来的当前数据条数
		String limitStr = request.getParameter("limit"); // 每页显示的数据数
		
		int start = Integer
		.parseInt(StringUtils.isNotBlank(startStr) ? startStr : "0");
		int limit = Integer
		.parseInt(StringUtils.isNotBlank(limitStr) ? limitStr : "30");
		int end = start + limit; // 当前页需要显示的数据 start--> end
		List<TimingToDevice> cardLs = remoteOperatorService.findTimingToDevice();
		int total = cardLs.size();
		if (end > total)
			end = total;

		JsonValueProcessor jsonProcessor = new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss");
		JsonConfig config = new JsonConfig(); 
		config.registerJsonValueProcessor(Date.class, jsonProcessor);
		
		GridDataModel<TimingToDevice> page = new GridDataModel<TimingToDevice>();
		page.setPage(start / limit + 1);
		page.setRows(cardLs);
		page.setTotal(total);
		String jsonStr = JSONObject.fromObject(page, config).toString();
		writeToResponse(jsonStr);
	}
	
	public void deleteCardTD()
	{
		String cardId = request.getParameter("jobId");
		if(StringUtils.isBlank(cardId)){
			writeToResponse("0");
		}else{
			remoteOperatorService.deleteCardTd(Long.valueOf(cardId));
			writeToResponse("1");
		}
	}
	
	public void deletePictureTD()
	{
		String pictureId = request.getParameter("jobId");
		if(StringUtils.isBlank(pictureId)){
			writeToResponse("0");
		}else{
			remoteOperatorService.deletePictureTd(Long.valueOf(pictureId));
			writeToResponse("1");
		}
	}
	
	public void deleteTimingTD()
	{
		String timingId = request.getParameter("jobId");
		if(StringUtils.isBlank(timingId)){
			writeToResponse("0");
		}else{
			remoteOperatorService.deleteTimingTd(Long.valueOf(timingId));
			writeToResponse("1");
		}
	}
	
	public void saveCardTD()
	{
		String jobModeId = request.getParameter("jobModeId");
		String guid = catd.getGuid();
		if (StringUtils.isNotBlank(guid)) {
			List<Device> devices = deviceService.findByAnyId(guid);
			List<CardLogToDevice> ctds = new ArrayList<CardLogToDevice>();
			for(Device device : devices){
				CardLogToDevice job = new CardLogToDevice();
				job.setObjectName(device.getName());
				job.setCreateDatetime(new Date());
				job.setStartTime(catd.getStartTime());
				job.setEnable(catd.getEnable());
				job.setRouter(deviceService.findRouter(device));
				job.setCardDatetime(catd.getCardDatetime());
				job.setCardDatetimeEnd(catd.getCardDatetimeEnd());
				job.setGuid(device.getGuid());
				job.setJobMode(upgradeToDeviceService.findJobModeById(StringUtils
						.isNotBlank(jobModeId) ? Long.valueOf(jobModeId) : 0));
				job.setJobStatus(upgradeToDeviceService.findjobStatusById(Long.valueOf(100L)));
				Users user = (Users) session.get("user");
				if (user != null) {
					job.setUserId(user.getId());
					job.setUserName(user.getName());
				} 
				ctds.add(job);
			}
			try {
				remoteOperatorService.saveCardAll(ctds);
				writeToResponse("1");
			} catch (Exception e) {
				e.printStackTrace();
				writeToResponse("0");
			}
		}
	}
	public void savePictureTD()
	{
		String jobModeId = request.getParameter("jobModeId");
		String guid = pitd.getGuid();
		if (StringUtils.isNotBlank(guid)) {
			List<Device> devices = deviceService.findByAnyId(guid);
			List<PictureLogToDevice> ptds = new ArrayList<PictureLogToDevice>();
			for(Device device : devices){
				PictureLogToDevice job = new PictureLogToDevice();
				job.setObjectName(device.getName());
				job.setCreatDatetime(new Date());
				job.setStartTime(pitd.getStartTime());
				job.setEnable(pitd.getEnable());
				job.setRouter(deviceService.findRouter(device));
				job.setGuid(device.getGuid());
				job.setFileName(pitd.getFileName());
				job.setPictureDatetime(pitd.getPictureDatetime());
				job.setJobMode(upgradeToDeviceService.findJobModeById(StringUtils
						.isNotBlank(jobModeId) ? Long.valueOf(jobModeId) : 0));
				job.setJobStatus(upgradeToDeviceService.findjobStatusById(Long.valueOf(100L)));
				Users user = (Users) session.get("user");
				if (user != null) {
					job.setUserId(user.getId());
					job.setUserName(user.getName());
				} 
				ptds.add(job);
			}
			try {
				remoteOperatorService.savePicAll(ptds);
				writeToResponse("1");
			} catch (Exception e) {
				e.printStackTrace();
				writeToResponse("0");
			}
		}
	}
	public void saveTimingTD()
	{
		String jobModeId = request.getParameter("jobModeId");
		String guid = titd.getGuid();
		if (StringUtils.isNotBlank(guid)) {
			List<Device> devices = deviceService.findByAnyId(guid);
			List<TimingToDevice> ttds = new ArrayList<TimingToDevice>();
			for(Device device : devices){
				TimingToDevice job = new TimingToDevice();
				job.setRouter(deviceService.findRouter(device));
				job.setObjectName(device.getName());
				job.setCreateDatetime(new Date());
				job.setEnable(titd.getEnable());
				job.setGuid(device.getGuid());
				job.setJobMode(upgradeToDeviceService.findJobModeById(StringUtils
						.isNotBlank(jobModeId) ? Long.valueOf(jobModeId) : 0));
				job.setJobStatus(upgradeToDeviceService.findjobStatusById(Long.valueOf(100L)));
				Users user = (Users) session.get("user");
				if (user != null) {
					job.setUserId(user.getId());
					job.setUserName(user.getName());
				}
				ttds.add(job);
			}
			try {
				remoteOperatorService.saveTimeAll(ttds);
				writeToResponse("1");
			} catch (Exception e) {
				e.printStackTrace();
				writeToResponse("0");
			}
		}
	}
	

	public CardLogToDevice getCatd() {
		return catd;
	}
	public void setCatd(CardLogToDevice catd) {
		this.catd = catd;
	}
	public PictureLogToDevice getPitd() {
		return pitd;
	}
	public void setPitd(PictureLogToDevice pitd) {
		this.pitd = pitd;
	}
	public TimingToDevice getTitd() {
		return titd;
	}
	public void setTitd(TimingToDevice titd) {
		this.titd = titd;
	}
}
